from datetime import datetime

from django.db import models


# Create your models here.
class Type(models.Model):
    """
    产品分类表
    """
    id = models.AutoField(verbose_name="序号", primary_key=True)
    type_name = models.CharField(verbose_name="产品类型", max_length=20)

    # 设置返回值
    def __str__(self):
        return self.type_name

    class Meta:
        db_table = "demo_product_type"
        verbose_name = '产品分类'
        verbose_name_plural = verbose_name


class Product(models.Model):
    """
    产品信息表
    """
    id = models.AutoField(verbose_name="序号", primary_key=True)
    name = models.CharField(verbose_name="名称", max_length=50)
    weight = models.CharField(verbose_name="重量", max_length=20)
    size = models.CharField(verbose_name="尺寸", max_length=20)
    publish = models.DateTimeField(default=datetime.now, verbose_name="发布日期")
    type = models.ForeignKey(Type, on_delete=models.CASCADE, verbose_name="产品类型")

    # 设置返回值
    def __str__(self):
        return self.name

    class Meta:
        ordering = ("-publish",)
        db_table = "demo_product"
        verbose_name = '产品信息'
        verbose_name_plural = verbose_name


class PersonInfo(models.Model):
    """
    人员信息表
    """
    id = models.AutoField(verbose_name="序号", primary_key=True)
    name = models.CharField(verbose_name="姓名", max_length=20)
    age = models.IntegerField(verbose_name="年龄")
    hireDate = models.DateField(null=True, blank=True, verbose_name="录用日期")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    def __str__(self):
        return self.name

    class Meta:
        # 根据创建时间倒序显示
        ordering = ['-created_at']
        db_table = "demo_personinfo"
        verbose_name = '人员信息'
        verbose_name_plural = verbose_name


class Vocation(models.Model):
    id = models.AutoField(verbose_name="序号", primary_key=True)
    job = models.CharField(verbose_name="岗位", max_length=20)
    title = models.CharField(verbose_name="工作内容简介", max_length=20)
    payment = models.IntegerField(verbose_name="薪水", null=True, blank=True)
    name = models.ForeignKey(PersonInfo, on_delete=models.Case, verbose_name="人员信息")

    def __str__(self):
        return str(self.id)

    class Meta:
        db_table = "demo_vocation"
        verbose_name = '职业信息'
        verbose_name_plural = verbose_name
